package com.flca.frw.util
{
	import com.flca.frw.components.DateInput;
	import com.flca.frw.components.TimeInput;
	
	import mx.controls.CheckBox;
	import mx.controls.TextArea;
	import mx.controls.TextInput;
	import mx.core.UIComponent;
	
	public class ConversionUtil 
	{
		public static function dateToString(aSource : Date) : String 
		{
			if (aSource != null) {
				var dd : int = aSource.date;
				var mm : int = aSource.month + 1;
				var jjjj : int = aSource.fullYear;
				var ddStr : String = dd.toString();
				var mmStr : String = mm.toString();
				if (dd < 10) ddStr = "0" + dd;
				if (mm < 10) mmStr = "0" + mm;
				return ddStr + mmStr + jjjj;
			} else {
				return "";
			}
		}

		public static function stringToDate(aSource : String) : Date 
		{
			return DateUtil.parseDate(aSource);
 		}
		
		public static function numberToString(aSource : Number) : String 
		{
			var result:String = aSource.toString();
			if (result == "NaN") {
				return "";
			} else {
				return result;
			}
		}

		public static function stringToNumber(aSource : String) : Number 
		{
			if (aSource != null) {
				var result:Number = Number(aSource);
				return result;
			} else {
				return new Number();
			}
		}
		
		public static function updateField(fld:UIComponent, value:Object, setter:Function, m2v:Boolean): void {
			if (m2v) {
				updateFieldValue(fld, value);
			} else {
				updateModelValue(fld, value, setter);
			}
		}
		
		private static function updateFieldValue(fld:UIComponent, value:Object): void {
			if (value is Date && fld is TextInput) {
				(fld as TextInput).text = dateToString(value as Date);
			} else if (value is Number && fld is TextInput) {
				(fld as TextInput).text = numberToString(value as Number);
			} else if (value is Boolean && fld is CheckBox) {
				(fld as CheckBox).selected = value as Boolean;
			} else if (fld is TextInput) {
				if (value != null) {
					(fld as TextInput).text = value as String;
				} else {
					(fld as TextInput).text = "";
				}
			} else if (fld is TextArea) {
				if (value != null) {
					(fld as TextArea).text = value as String;
				} else {
					(fld as TextArea).text = "";
				}
			} else {
				//Alert.show("updateFieldValue() failed
			}
		}

		private static function updateModelValue(fld:UIComponent, value:Object, setter:Function): void 
		{
			if (setter == null || fld == null) {
				return;
			}
			
			if (fld is DateInput) {
				setter(stringToDate((fld as TextInput).text));
			} else if (value is Date && fld is TextInput) {
				setter(stringToDate((fld as TextInput).text));
			} else if (value is Number && fld is TextInput) {
				setter(stringToNumber((fld as TextInput).text));
			} else if (value is Boolean && fld is CheckBox) {
				setter((fld as CheckBox).selected);
			} else if (fld is TimeInput) {
				setter(stringToDate((fld as TextInput).text));
			} else if (fld is TextInput) {
				setter((fld as TextInput).text);
			} else if (fld is TextArea) {
				setter((fld as TextArea).text);
			} else {
				//Alert.show("updateFieldValue() failed
			}
		}
		
		public static function getNumber(aNumber : Number) : Number {
			if (aNumber) {
				return aNumber;
			} else {
				return 0;
			}
		}
	}
}